package com.ihr360.insurance.controller.benefit;

import com.ihr360.authority.sharing.annonation.ControlMode;
import com.ihr360.authority.sharing.annonation.RestControl;
import com.ihr360.authority.sharing.privilege.AccessControlMethod;
import com.ihr360.commons.vo.PageData;
import com.ihr360.entity.StaffBankSalaryEntity;
import com.ihr360.insurance.entity.request.StaffBenefitRequest;
import com.ihr360.insurance.entity.request.StaffBenefitVoRequest;
import com.ihr360.insurance.entity.request.StaffIdRequest;
import com.ihr360.insurance.entity.vo.StaffBenefitEntity;
import com.ihr360.insurance.entity.vo.StaffBenefitVo;
import com.ihr360.insurance.model.dto.staff.StaffBenefit;
import com.ihr360.insurance.service.StaffBenefitService;
import com.ihr360.maven.plugin.ModuleCodeStore;
import com.ihr360.maven.plugin.annotation.FunctionDefinition;
import com.ihr360.maven.plugin.annotation.ModuleDefinition;
import com.ihr360.rest.service.SimpleRestService;
import com.ihr360.rest.webmvc.AbstractRestSimpleWithLongIdController;
import com.ihr360.shared.advice.response.DisableResponseAdvice;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Stone.Shi
 * @description
 * @date 2018-02-01 18:36:42.
 */
@RestController
@RequestMapping("/staffBenefits")
@ModuleDefinition(mainModuleCode = ModuleCodeStore.Module.BENEFIT_CODE,
        mainModuleName = ModuleCodeStore.Module.BENEFIT_NAME,
        subModuleCode = ModuleCodeStore.Module.Benefit.STAFF_SIHF_ARCHIVE_CODE,
        subModuleName = ModuleCodeStore.Module.Benefit.STAFF_SIHF_ARCHIVE_NAME,
        hasExtendsAbstractRestController = true)
@DisableResponseAdvice
@RestControl(mode = ControlMode.ALLOW, controls = AccessControlMethod.METHODS.search)
public class StaffBenefitController extends AbstractRestSimpleWithLongIdController<StaffBenefit> {

    @Autowired
    private StaffBenefitService staffBenefitService;

    @Override
    public SimpleRestService<StaffBenefit, Long> getSimpleRestService() {
        return staffBenefitService;
    }

    /**
     * feign接口
     */
    @FunctionDefinition(functionName = "保存员工的社保公积金相关数据", functionCode = ModuleCodeStore.Function.UPDATE)
    @RequestMapping(value = "/staff/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public void save(@RequestParam String companyId,
                     @RequestBody StaffBenefitVoRequest request) {
        staffBenefitService.saveBenefits(companyId, request.getBenefitVos(), null, request.getStaffIds());
    }

    /**
     * feign接口
     */
    @FunctionDefinition(functionName = "获取指定范围内的员工社保公积金数据", functionCode = ModuleCodeStore.Function.VIEW)
    @RequestMapping(value = "/listin", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public List<StaffBenefitVo> getAllStaffBenefitInclude(@RequestParam String companyId,
                                                          @RequestBody StaffIdRequest staffIdRequest) {
        return staffBenefitService.getAllStaffBenefitInclude(companyId, staffIdRequest.getStaffIds());
    }

    /**
     * feign接口
     */
    @FunctionDefinition(functionName = "获取指定id范围内的符合条件的员工社保公积金数据", functionCode = ModuleCodeStore.Function.VIEW)
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public PageData<StaffBenefitVo> getList(@RequestParam String companyId,
                                            @RequestBody StaffBenefitRequest request) {
        return staffBenefitService.getList(companyId, request.getStaffIds(), request);
    }

    //******************************花名册编辑社保公积金数据接口**********************************/

    @FunctionDefinition(functionName = "获取员工社保公积金基本数据", functionCode = ModuleCodeStore.Function.VIEW)
    @ApiOperation(value = "花名册获取员工的社保公积金基本数据")
    @RequestMapping(value = "/baseInfo/query", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public StaffBenefitEntity getStaffBenefitInfo(@RequestParam String companyId,
                                                  @RequestParam String staff) {
        return staffBenefitService.getStaffBenefitInfo(companyId, staff);
    }

    @FunctionDefinition(functionName = "保存员工社保公积金基本数据", functionCode = ModuleCodeStore.Function.UPDATE)
    @ApiOperation(value = "花名册保存员工的社保公积金基本数据")
    @RequestMapping(value = "/baseInfo/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public void saveStaffBenefitInfo(@RequestParam String companyId,
                                     @RequestBody StaffBenefitEntity entity) {
        staffBenefitService.saveStaffBenefitInfo(companyId, entity);
    }

    @FunctionDefinition(functionName = "花名册离职最后缴纳月数据保存", functionCode = ModuleCodeStore.Function.UPDATE, isPublic = true)
    @ApiOperation(value = "花名册离职最后缴纳月数据保存")
    @RequestMapping(value = "/baseInfo/staffQuit", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public void saveStaffBenefitEndMonth(@RequestParam String companyId,
                                         @RequestBody StaffBenefitEntity entity) {
        staffBenefitService.saveStaffBenefitEndMonth(companyId, entity);
    }

    @FunctionDefinition(functionName = "保存公司员工社保福利信息", functionCode = ModuleCodeStore.Function.UPDATE, isPublic = true)
    @ApiOperation(value = "保存公司员工社保福利信息（数据修复使用）")
    @RequestMapping(value = "/saveAllInsurance", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public void saveAllInsurance(@RequestParam String companyId,
                                 @RequestBody List<StaffBankSalaryEntity> staffBankSalaryEntityList) {
        staffBenefitService.saveAllInsurance(companyId, staffBankSalaryEntityList);
    }
}
